﻿package com.victory.properties
{
	
	/**
	 * ...
	 * @author ...
	 */
	public class Player 
	{
		import mx.controls.Alert;
		
		[Bindable] public var HP:int; // Current HP
		[Bindable] public var maxHP:int; // Max HP
		[Bindable] public var Pdefense:int; // Create defenses - this is physical
		[Bindable] public var Edefense:int; // Create defenses - this is elemental
		[Bindable] public var Cdefense:int; // Create defenses - this is corporeal
		[Bindable] public var Sdefense:int; // Create defenses - this is spiritual
		[Bindable] public var AP:int; // Current AP
		[Bindable] public var apRegen:int; // AP Regen rate, per round.
		[Bindable] public var maxAP:int; // Maximum AP that a player has.
		[Bindable] public var name:String; // Name of the player.
		[Bindable] public var status:Array; // Array of status effects currently active. Array of status objects.
		[Bindable] public var isBlocking:Boolean = false; // This should be replaced by a status for blocking.
		[Bindable] public var tactics:Array = new Array(); // Array of chosen tactics. Should be defined by the character
		[Bindable] public var equipment:Array = new Array(); // Equipment has 4 parts.
		[Bindable] public var attacks:Array = new Array(); // Array of attacks, in order
		[Bindable] public var attack1:Attack;
		[Bindable] public var attack2:Attack;
		[Bindable] public var attack3:Attack;
		
		public function Player(xml:XML) {
			// Constructor method. Will also need to pull the XML for a character. Maybe that will be passed, instead of a name.
			initialize(xml);
		}
		
		public function initialize(charXML:XML):void {
			// Set values found in the XML.
			// Set name and constant values
			name = charXML.name;
			maxHP = charXML.HP;
			maxAP = charXML.AP;
			apRegen = charXML.APregen;
			Pdefense = charXML.physical;
			Edefense = charXML.elemental;
			Cdefense = charXML.corporeal;
			Sdefense = charXML.spiritual;
			
			// Create equipment array
			var equip:XMLList = charXML.equipment;
			var weapon:Equipment = new Equipment("weapon", equip.weapon.name, equip.weapon.effect, equip.weapon.attributes);
			var armor:Equipment = new Equipment("armor", equip.armor.name, equip.armor.effect, equip.armor.attributes);
			var misc1:Equipment = new Equipment("misc1", equip.misc1.name, equip.misc1.effect, equip.misc1.attributes);
			var misc2:Equipment = new Equipment("misc2", equip.misc2.name, equip.misc2.effect, equip.misc2.attributes);
			equipment["weapon"] = weapon;
			equipment["armor"] = armor;
			equipment["misc1"] = misc1;
			equipment["misc2"] = misc2;
			incrementItemStats(equipment["weapon"]);
			incrementItemStats(equipment["armor"]);
			incrementItemStats(equipment["misc1"]);
			incrementItemStats(equipment["misc2"]);
			// Create attacks array
			attack1 = new Attack(charXML.attacks.attack1);
			attack2 = new Attack(charXML.attacks.attack2);
			attack3 = new Attack(charXML.attacks.attack3);
			attacks[0] = attack1;
			attacks[1] = attack2;
			attacks[2] = attack3;
			
			// Create tactics array
			//var tactic1:Tactic = new Tactic(charXML.tactics.tactic1);
			//var tactic2:Tactic = new Tactic(charXML.tactics.tactic2);
			//changeTactic(1, tactic1);
			//changeTactic(2, tactic2);
		
			// Set variables to constant values for initialize
			AP = maxAP;
			HP = maxHP;
		}
		
		public function incrementItemStats(item:Equipment):void {
			maxHP += int(item.attributes["HP"]);
			maxAP += int(item.attributes["HP"]);
			apRegen += int(item.attributes["apRegen"]);
			Pdefense += int(item.attributes["Pdefense"]);
			Edefense += int(item.attributes["Edefense"]);
			Cdefense += int(item.attributes["Cdefense"]);
			Sdefense += int(item.attributes["Sdefense"]);
		}
		
		public function changeTactic(tacNum:int, tactic:Tactic):void {
			tactics[tacNum] = tactic
		}
		
		public function update():void {
			regenAP(apRegen); // Regenerate AP
			
			
		}
		
		public function regenAP(regen:int):void {
			AP += regen;
			if (AP > maxAP) {
				AP = maxAP;
			}
		}
		
		public function checkHP():Boolean {
			// Returns true if HP is greater than 0. Else, returns false.
			// This part also includes any beginning of the turn status / damage / regen
			if (HP <= 0) {
				return false;
			} else {
				return true;
			}
		}
		
	}
	
}